package org.example.cloudprviderpayment8001.controller;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import org.example.cloudprviderpayment8001.annotion.DataSource;
import org.example.cloudprviderpayment8001.annotion.Limit;
import org.example.cloudprviderpayment8001.annotion.Timeout;
import org.example.cloudprviderpayment8001.annotion.TimeoutBack;
import org.example.cloudprviderpayment8001.pojo.Admin;
import org.example.cloudprviderpayment8001.service.StudentService;
import org.example.cloudprviderpayment8001.service.imp.AdminServiceImp;
import org.example.cloudprviderpayment8001.service.imp.StudentServiceImp;
import org.example.cloudprviderpayment8001.util.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/student")
public class controller {
    @Autowired
    private StudentService StudentService;
    @Autowired
    private AdminServiceImp adminServiceImp;

    @GetMapping("/findAllByIdXml")
    public BaseResult findAllByIdXml(@RequestParam("name") String name){
        return BaseResult.ok(StudentService.findAllByIdXml(name));
    }

    @GetMapping("/findAdd")
    public BaseResult findStudentAll(){
        return BaseResult.ok(StudentService.findAll());
    }

    @GetMapping("/findStudent")
    public BaseResult findStudent(@RequestParam("id") Integer id){
        return BaseResult.ok(StudentService.findAllStudent(id));
    }
    @GetMapping("/second3")
    @Timeout(retry = 3)
    public BaseResult findStudent2(@RequestParam("id") Integer id){
        return BaseResult.ok(StudentService.findAllStudentOne(id));
    }

    @GetMapping("/second1")
    @Timeout(retry = 3)
    public BaseResult findStudentById(@RequestParam("id") Integer id,
                                  @RequestParam("name") String name){
        return BaseResult.ok(StudentService.findAllStudentById(id,name));
    }
    @GetMapping("/second")
    @Timeout(retry = 3)
    public BaseResult findStudent(@RequestParam("age") int age,
                                  @RequestParam("name") String name){
        return BaseResult.ok(StudentService.findAllStudentSecond(age,name));
    }

    @DeleteMapping("/delete")
    public void deleteById(@RequestParam(value = "id") Integer id){
        StudentService.deleteById(id);
    }

    @TimeoutBack(
            value = 2,
            unit = TimeUnit.SECONDS,
            fallback = "fallbackQuery",
            retry = 3,
            retryDelay = 3000
    )
    @GetMapping("/query")
    public BaseResult timeout() throws Throwable {
        TimeUnit.SECONDS.sleep(new Random().nextInt(6));
        return BaseResult.ok("请求成功");
    }

    public BaseResult fallbackQuery(Throwable e){
        return BaseResult.ok("超时");
    }
    @GetMapping("/data")
    public BaseResult data(){
        AdminServiceImp imp = new AdminServiceImp();
        imp.running();
        DynamicDataSourceContextHolder.push("shopping");
        List<Admin> admins = adminServiceImp.getBaseMapper().selectList(null);
        System.out.println(admins);
        return BaseResult.ok(admins);
    }

    @GetMapping("/data1")
    public BaseResult data1(){
        AdminServiceImp imp = new AdminServiceImp();
        imp.running();
        return BaseResult.ok(imp.running2());
    }

}
